/*
  Copyright (c) 2014 Dimitrij Gester

  "Smart RC TX" is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  "Smart RC TX" is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General
  Public License along with "Smart RC TX"; if not, write to the
  Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  Boston, MA  02111-1307  USA
*/

package com.codeplex.smartrctx.configurator.main.controller;

import java.util.Observable;
import java.util.Observer;

import android.widget.TextView;

public abstract class BaseSetter implements Setter, Observer {

	protected TextView target;
	protected int channel;

	public BaseSetter(TextView v, int i) {
		if (v == null)
			throw new NullPointerException();
		target = v;
		channel = i;
	}

	protected abstract Integer[] getData();

	@Override
	public void setTargetEditor(TextView t) {
		target = t;
	}
	
	public void setChannel(int channel) {
		this.channel = channel;
	}

	@Override
	public void setValue(String v) {
		Integer[] data = getData();
		data[channel] = null;
		try {
			data[channel] = Integer.valueOf(v);
		} catch (NumberFormatException e) {
		}
	}

	public void updateView() {
		Integer[] data = getData();

		if (target != null) {
			String t = "";
			if (data[channel] != null) {
				t = data[channel].toString();
			}
			target.setText(t);
		}
	}

	@Override
	public void update(Observable observable, Object data) {
		updateView();
	}
}